package shop.web.controller.front.activity;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.FrontBaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.services.front.activityJoin.ActivityJoinService;
import shop.services.front.activityJoin.bean.ActivityJoin;
import shop.services.front.address.AddressService;
import shop.services.front.address.bean.Address;
import shop.services.front.order.OrderService;
import shop.services.front.order.bean.Order;
import shop.services.front.activity.ActivityService;
import shop.services.front.activity.bean.Activity;
import shop.services.front.orderdetail.OrderdetailService;
import shop.services.front.orderdetail.bean.Orderdetail;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/4/17.
 */
@Controller
@RequestMapping("/activity")
public class ActivityController extends FrontBaseController<Activity> {
    @Resource
    private ActivityService activityService;
    @Resource
    private ActivityJoinService activityJoinService;
    @Resource
    private OrderService orderService;
    @Resource
    private OrderdetailService orderdetailService;
    @Resource
    private AddressService addressService;
    @Override
    public Services<Activity> getService() {
        return activityService;
    }

    /**
     * 跳转到参团（开团）列表页面方法
     *
     * @return
     */
    @RequestMapping("/toActivityList")
    public String toActivityList() {
        return "/front/activity/activityList";
    }

    /**
     * 跳转到参团（开团）详情页面方法
     * @return
     */
    @RequestMapping("/toActivityJoinList")
    public String toActivityJoin() {
        return "/front/activity/activityJoin";
    }

    /**
     * 跳转到添加订单页面方法
     * @return
     */
    @RequestMapping("/toInsertOrder")
    public String toInsertOrder() {
        return "/front/activity/insertOrder";
    }

    /**
     * 跳转到添加地址页面方法
     * @return
     */
    @RequestMapping("/toInsertAddress")
    public String toInsertAddress() {
        return "/front/activity/insertOrder";
    }

    /**
     * 跳转到用户地址列表页面方法
     * @return
     */
    @RequestMapping("/toAddressList")
    public String toAddressList() {
        return "/front/activity/addressList";
    }

    /**
     * 查询参团列表方法
     * @param activity
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/selectActivityJoinList",method=RequestMethod.GET)
    @ResponseBody
    public JSONResult selectActivityJoinList(Activity activity) throws Exception{
        JSONResult jsonResult =new JSONResult();
        jsonResult.setData(activityService.selectActivityJoin(activity));
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 查询参团详情方法
     *
     * @param activity
     * @return
     */
    @RequestMapping(value = "/selectActivityJoinDetail", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectActivityJoinDetail(Activity activity) {
        jsonResult = new JSONResult();
        Activity rs = activityService.selectOne(activity);
        jsonResult.setData(rs);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 立即开团方法
     * @param activityJoin
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/insertActivityJoin",method= RequestMethod.POST)
    @ResponseBody
    public JSONResult insertActivityJoin(ActivityJoin activityJoin) throws Exception{
        JSONResult jsonResult=new JSONResult();
        Activity activity = new Activity();
        activity.setId(String.valueOf(activityJoin.getActivityID()));
        activity = activityService.selectOne(activity);
        if(activity.getCountNum()<activity.getLimits()&&activity.getType().equals("0")){//判断人数是否满足限制
            activityJoinService.insert(activityJoin);
            activityJoin.setRoleType("0");
            activityJoinService.update(activityJoin);//团长标志
            activity.setCountNum(activity.getCountNum()+1);
            activityService.update(activity);//更新开团参与人数
            activity.setId(null);
            activity.setType("1");
            activity.setCountNum(1);
            activityService.insert(activity);//参团列表增加一条记录
            jsonResult.setSuccess(true);
            return jsonResult;
        }else{
            jsonResult.setMessage("活动人数已满！期待下次参与！");
            jsonResult.setSuccess(false);
            return jsonResult;
        }

    }

    /**
     * 立即参团方法
     * @param activityJoin
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/insertActivityJoins",method=RequestMethod.POST)
    @ResponseBody
    public JSONResult insertActivityJoins(ActivityJoin activityJoin) throws Exception{
        JSONResult jsonResult = new JSONResult();
        Activity activity = new Activity();
        activity.setId(String.valueOf(activityJoin.getActivityID()));
        activity = activityService.selectOne(activity);
        if(activity.getCountNum()<activity.getLimits()&&activity.getType().equals("1")){//判断人数是否满足限制
            activityJoinService.insert(activityJoin);
            activityJoin.setRoleType("1");
            activityJoinService.update(activityJoin);//参与者标志
            activity.setCountNum(activity.getCountNum()+1);
            activityService.update(activity);//更新参与人数
            if(activity.getCountNum()==activity.getLimits()){
                jsonResult.setMessage("参与团购成功，商品正在配送！");
            }
            jsonResult.setSuccess(true);
            return jsonResult;
        }else{
            jsonResult.setMessage("活动人数已满,期待下次参与！");
            jsonResult.setSuccess(false);
            return jsonResult;
        }
    }

    /**
     * 新增订单方法
     * @param order
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/insertOrder",method=RequestMethod.POST)
    @ResponseBody
    public JSONResult insertOrder(Order order) throws Exception{
        JSONResult jsonResult =new JSONResult();
        orderService.insert(order);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 订单详情录入方法
     * @param orderdetail
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/insertOrderDetail",method=RequestMethod.POST)
    @ResponseBody
    public JSONResult insertOrderDetail(Orderdetail orderdetail) throws Exception{
        JSONResult jsonResult =new JSONResult();
        orderdetailService.insert(orderdetail);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 新增用户地址方法
     * @param address
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/insertAddress",method=RequestMethod.POST)
    @ResponseBody
    public JSONResult insertAddress(Address address) throws Exception{
        JSONResult jsonResult =new JSONResult();
        int amount =addressService.selectList(address).size();
        if(amount==0){
            address.setIsdefault("y");
        }
        addressService.insert(address);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 查询用户地址列表方法
     * @param address
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/selectAddressList",method=RequestMethod.GET)
    @ResponseBody
    public JSONResult selectAddressList(Address address) throws Exception{
        JSONResult jsonResult =new JSONResult();
        jsonResult.setData(addressService.selectList(address));
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 根据用户查询团购列表
     * @param activityJoin 用户活动参与表
     * @return 该用户参与的活动
     */
    @RequestMapping(value = "/selectActivityList",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectActivityList(ActivityJoin activityJoin){
        JSONResult rs=new JSONResult();
        List<ActivityJoin> activityJoinList=activityJoinService.selectListByAccount(activityJoin);
        rs.setData(activityJoinList);
        rs.setSuccess(true);
        return rs;
    }

}